<%--
    Document   : validation
    Created on : Mar 31, 2010, 12:46:24 PM
    Author     : Shiag
--%>
<%@page import="powerpass.PowerPassSQL" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Validation</title>
        <link rel="icon" type="image/gif" href="images/plogo.gif" />
        <jsp:include page="/background.jsp"/>
        <style type="text/css">

    a{
        outline: none;
    }

    a:active{
        outline: none;
    }

    #forgotPass{
    text-align:left;
    }

    #forgotPass h2{
        color:fuchsia;
        font-size:110%;
        font-family:arial;
        margin:10px 10px 10px 10px;
        font-weight:bold !important;
    }

    #forgotPass h2 span{
        font-size:125%;
        font-weight:normal !important;
    }

    #forgotPass ul{
        margin:0px 0px 0px 0px;
        padding:0px 0px 0px 0px;
    }

    #forgotPass li{
        margin:0px 10px 3px 10px;
        padding:2px;
        list-style-type:none;
        display:block;
        width:177px;
        color:#FF0000;
    }

    #forgotPass a{
        width:100%;
    }

    #forgotPass a:link,
    #forgotPass a:visited{
        color:fuchsia;
        font-family:verdana;
        font-size:100%;
        text-decoration:none;
        display:block;
        margin:0px 0px 0px 0px;
        padding:0px;
        width:100%;
    }

    #forgotPass a:hover{
        color:#FF0000;
        text-decoration:underline;
    }

    #forgotPass{
        border-width: 5px;
        border-color: #000000;
        position: absolute;
        width: 400px;
        height: 320px;
        top: 33%;
        left:33%;
    }

    #forgotPassContents{
        float:left;
        overflow:hidden !important;
        width:400px;
        height:320px;
    }

    #forgotPassContentsInner{
        width:350px;
    }
            </style>
    </head>
    <body>
        <%@page language="java"%>
        <%@page import="java.sql.*"%>
        <%
ResultSet rs = null;
String name = request.getParameter( "txtLoginName" );
session.setAttribute( "theName", name );
String pass = request.getParameter( "textPassword" );
session.setAttribute( "thePass", pass );
PowerPassSQL pp = new PowerPassSQL();
String newSQL;
session.setAttribute("theMessage", "");
try
{
    if(name == null)
                {
                        String message = "Your username was not found.\n Your username is the email" +
                                " address you signed up with.";
                        session.setAttribute("theMessage", message);
                        String redirectURL = "login.jsp";
                        response.sendRedirect(redirectURL);
                        }
    if(name.equals(""))
                {
                        String message = "Your username was not found.\n Your username is the email" +
                                " address you signed up with.";
                        session.setAttribute("theMessage", message);
                        String redirectURL = "login.jsp";
                        response.sendRedirect(redirectURL);
                        }
    newSQL = "SELECT email_address, password, clientid from clients WHERE email_address ='"+name+"';";
                    rs = pp.selectInfo(newSQL);
                    rs.next();
    //out.print("try\n");
        String foundName = rs.getString(1);
        String foundPass = rs.getString(2);
        
        if(request.getHeader("referer").contains("/login.jsp") && request.getHeader("referer") != null && request.getHeader("referer").isEmpty() == false)
        {
      //      out.print("login\n");
            int client = rs.getInt(3);
            session.setAttribute("theClient",client);
            if(name == "" || pass == "" || name == null || pass == null)
            {
        //        out.print("name,pass empty or null\n");
                String redirectURL = "login.jsp";
                response.sendRedirect(redirectURL);
            }
            if(foundName.equals(name))
            {
          //      out.print("email match found");
                if(foundPass.equals(pass))
                {
            //        out.print("pass match found");
                    String browser = (String)request.getHeader("User-Agent");
                    java.text.DateFormat dateFormat = new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                    java.util.Date date = new java.util.Date();
                    newSQL = "INSERT INTO loginhistory(clientid,logtype,logindt,browser)" +
                            "VALUES("+client+",'LogIn','"+ dateFormat.format(date)+"','"+browser+"');";
                    pp.insertInfo(newSQL);
                    String redirectURL = "index.jsp";
                    response.sendRedirect(redirectURL);
                }
                else
                {
                        String message = "Your Password does not match.\n If you've forgotten your password" +
                                " click on the \"Forgot password link\"";
                        session.setAttribute("theMessage", message);
                        String redirectURL = "login.jsp";
                        response.sendRedirect(redirectURL);
                        }
            }
        }
         if(request.getHeader("referer").contains("/forgotPassword.jsp") && request.getHeader("referer") != null && request.getHeader("referer").isEmpty() == false)
        {
              newSQL = "select q_text from questions where qid in(Select q1id from clients where email_address ='"+name+"');";
             rs = pp.selectInfo(newSQL);
             rs.next();
             %>
             <div id="forgotPass">
            <div id="forgotPassContentsInner">
             <form action="password.jsp">
                 <table>
                     <tr>
                         <td><%out.print(rs.getString(1));%></td><td><input type="text" name="answer1"></td>
                     </tr>
                     <%
             newSQL = "Select q_text from questions where qid in(Select q2id from clients where email_address ='"+name+"');";
             rs = pp.selectInfo(newSQL);
             rs.next();
                     %>
                     <tr>
                     <td><%out.print(rs.getString(1));%></td><td><input type="text" name="answer2"></td>
                     </tr>
                     <tr>
                         <td></td><td><input type="submit" value="Send" name="validate" id="validate"
        >
             </td>
                         <td>

                         </td>

                     </tr>
                 </table>
             </form>
            </div>
             </div>
        <%}
            else
                {
        //out.println("else");
                    String redirectURL = "login.jsp";
                    response.sendRedirect(redirectURL);
                }
}
catch (Exception e) {
//print error message
out.println("<center><font size=5> Error in inserting the data</font></center>");
out.println("the error is:"+e.toString());
}
   finally
           {
           //out.print("close Connection\n");
           pp.Close();
       }
        %>
    </body>
</html>